/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.Msg.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.Love.dto.LoveDemandDto;
import com.fzhucc.modules.Msg.domain.Msg;
import com.fzhucc.modules.Msg.domain.dto.AuditMsgDto;
import com.fzhucc.modules.Msg.domain.dto.AuditMsgPassDto;
import com.fzhucc.modules.Msg.domain.dto.SaveMsgDto;
import com.fzhucc.modules.Msg.service.MsgService;
import com.fzhucc.modules.Msg.domain.dto.MsgQueryCriteria;
import com.fzhucc.modules.sciEnterprise.domain.dto.AuditEnterpriseDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-06-13
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "留言管理")
@RequestMapping("/api/msg")
public class MsgController {

    private final MsgService msgService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('msg:list')")
    public void exportMsg(HttpServletResponse response, MsgQueryCriteria criteria) throws IOException {
        msgService.download(msgService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询留言管理")
    @ApiOperation("查询留言管理")
    @PreAuthorize("@el.check('msg:list')")
    public ResponseEntity<Object> queryMsg(MsgQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(msgService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/getByGlIdType")
    @Log("查询留言管理通过关联id及分类及分页")
    @ApiOperation("查询留言管理通过关联id及分类及分页")
    public ResponseEntity<Object> getByGlIdType(MsgQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(msgService.getByGlIdType(criteria,pageable), HttpStatus.OK);
    }

    /*@PostMapping
    @Log("新增留言管理")
    @ApiOperation("新增留言管理")
    @PreAuthorize("@el.check('msg:add')")
    public ResponseEntity<Object> createMsg(@Validated @RequestBody Msg resources){
        return new ResponseEntity<>(msgService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改留言管理")
    @ApiOperation("修改留言管理")
    @PreAuthorize("@el.check('msg:edit')")
    public ResponseEntity<Object> updateMsg(@Validated @RequestBody Msg resources){
        msgService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }*/

    @DeleteMapping
    @Log("删除留言管理")
    @ApiOperation("删除留言管理")
    //@PreAuthorize("@el.check('msg:del')")
    public ResponseEntity<Object> deleteMsg(@RequestBody Integer[] ids) {
        msgService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/save")
    @Log("新增留言")
    @ApiOperation("新增留言")
    public ResponseEntity<Object> saveMsg(@RequestBody SaveMsgDto dto){
        return new ResponseEntity<>(msgService.saveMsg(dto), HttpStatus.CREATED);
    }

    @PutMapping(value = "/audit")
    @Log("状态审核")
    @ApiOperation("状态审核")
    public ResponseEntity<Object> audit(@Validated @RequestBody AuditMsgDto dto) {
        msgService.audit(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/auditPass")
    @Log("一键审核通过")
    @ApiOperation("一键审核通过")
    public ResponseEntity<Object> auditPass(@Validated @RequestBody AuditMsgPassDto dto) {
        msgService.auditPass(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping(value = "/my")
    @Log("我的留言")
    @ApiOperation("我的留言")
    public ResponseEntity<Object> my(MsgQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(msgService.my(criteria, pageable), HttpStatus.OK);
    }

}